package com.wck.car.oyz;

import com.wck.car.model.CarBrandInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class CarBrandInfoType {
    public static List<CarBrandInfo> getHtml() {
        String body = SpiderCar.getBody("https://m.evpartner.com/zyc");
        // 打印响应内容
        Document document = Jsoup.parse(body);

        Elements elementsByClass = document.getElementsByClass("list-box");
        Element element = elementsByClass.get(0);
        Elements ul = element.getElementsByTag("ul");

        List<CarBrandInfo> brandInfos = new ArrayList<>();

        ul.forEach(e -> {
            Elements li = e.getElementsByTag("li");
            li.forEach(d -> {
                Element aTag = d.select("a[href]").get(0);
                CarBrandInfo carBrandInfo = new CarBrandInfo();
                carBrandInfo.setBrandName(aTag.text());
                carBrandInfo.setBrandLink(aTag.attr("href"));
                String id = aTag.attr("href").split("=")[1];
                carBrandInfo.setId(Long.parseLong(id));
                Element img = d.getElementsByTag("img").get(0);
                carBrandInfo.setBrandImg(img.attr("src"));
                brandInfos.add(carBrandInfo);
            });
        });
        return brandInfos;
    }
}
